/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.noteuser.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.noteuser.entity.NoteUser;
import com.thinkgem.jeesite.modules.noteuser.service.NoteUserService;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

/**
 * 用户增删改查Controller
 * @author liyutg
 * @version 2017-07-26
 * frontPath
 * adminPath
 */
@Controller
@RequestMapping(value = "/i/noteuser/noteUser")
public class NoteUserController extends BaseController {

	@Autowired
	private NoteUserService noteUserService;
	
	@ModelAttribute
	public NoteUser get(@RequestParam(required=false) String id) {
		NoteUser entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = noteUserService.get(id);
		}
		if (entity == null){
			entity = new NoteUser();
		}
		return entity;
	}
	
	@RequiresPermissions("noteuser:noteUser:view")
	@RequestMapping(value = {"list", ""})
	public String list(NoteUser noteUser, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<NoteUser> page = noteUserService.findPage(new Page<NoteUser>(request, response), noteUser); 
		model.addAttribute("page", page);
		return "modules/noteuser/noteUserList";
	}

	@RequiresPermissions("noteuser:noteUser:view")
	@RequestMapping(value = "form")
	public String form(NoteUser noteUser, Model model) {
		model.addAttribute("noteUser", noteUser);
		return "modules/noteuser/noteUserForm";
	}

	@RequiresPermissions("noteuser:noteUser:edit")
	@RequestMapping(value = "save")
	public String save(NoteUser noteUser, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, noteUser)){
			return form(noteUser, model);
		}
		noteUserService.save(noteUser);
		addMessage(redirectAttributes, "保存用户成功");
		return "redirect:"+Global.getAdminPath()+"/noteuser/noteUser/?repage";
	}
	
	@RequiresPermissions("noteuser:noteUser:edit")
	@RequestMapping(value = "delete")
	public String delete(NoteUser noteUser, RedirectAttributes redirectAttributes) {
		noteUserService.delete(noteUser);
		addMessage(redirectAttributes, "删除用户成功");
		return "redirect:"+Global.getAdminPath()+"/noteuser/noteUser/?repage";
	}

	//第一个 ok
	@RequestMapping(value = "noteUser")
	public String noteUser(HttpServletRequest request) {
		HttpSession session=request.getSession();
		session.setAttribute("userId",null);
		session.setAttribute("userName",null);
		return "modules/noteuser/Start_lode";
	}
	/*
		界面跳转
	 */
	// 2 ok
	@RequestMapping(value = "toMain")
	public String toMain() {
		return "modules/noteuser/Main";
	}
//第三个 ok
	@RequestMapping(value = "toeditLast")
	public String toeditLast() {
		return "modules/noteuser/editLast";
	}
//第四个   ok
	@RequestMapping(value = "tomynotes")
	public String tomynotes() {
		return "modules/noteuser/mynotes";
	}
	//第五个  ok
	@RequestMapping(value = "tonewnote")
	public String tonewnote() {
		return "modules/noteuser/newnote";
	}

	// 6  ok
	@RequestMapping(value = "toPasswd_forget")
	public String toPasswd_forget() {
		return "modules/noteuser/Passwd_forget";
	}
	// 7  ok
	@RequestMapping(value = "torecover")
	public String torecover() {
		return "modules/noteuser/recover";
	}

	//  8  ok
	@RequestMapping(value = "toregister_main")
	public String toregister_main() {
		return "modules/noteuser/register_main";
	}
	//  9  ok
	@RequestMapping(value = "toshow")
	public String toshow() {
		return "modules/noteuser/show";
	}





	//登录
	//@RequiresPermissions("noteuser:user:edit")
	//@RequiresPermissions("noteuser:noteUser:view")
	@RequestMapping(value = "login")
	@ResponseBody
	public Map<String,String> login(String userName, String password, HttpServletRequest request, HttpServletResponse response) {
		HttpSession session=request.getSession();
		String userId=(String) session.getAttribute("userId");

		Map<String,String> map=new HashMap<String, String>();
		logger.debug("login开始");
		if(userName!=null&&password!=null){
			//已登录返回登录值
			logger.debug("userId="+userId);
			/*
			* if( userId!=null){
				map.put("userid",userId);
				map.put("result","1");
				map.put("message","login success");
				return map;
			}
			else */
			if(noteUserService.check(userName,password)==1){
				userId=noteUserService.username_to_id(userName);
				map.put("userid",userId);
				map.put("result","1");
				map.put("message","login success");
				session.setAttribute("userId",userId);
				session.setAttribute("userName",userName);
			}
			else{
				map.put("userid","");
				map.put("result","0");
				map.put("message","login failed");
			}
		}else{
			map.put("userid","");
			map.put("result","3");
			map.put("message","parameter error");
		}
		return map;
	}
	//注册用户_发送验证码identifycode
	//@RequiresPermissions("noteuser:user:edit")
	@RequestMapping(value = "sendCodeRegister")
	@ResponseBody
	public Map<String,String> sendCodeRegister(String userName, String password,String mail,HttpServletRequest request, HttpServletResponse response) {
		//获取当前会话
		HttpSession session=request.getSession();

		Map<String,String> map=new HashMap<String, String>();
		if(userName!=null&&password!=null&&mail!=null){
			if(noteUserService.exist("user_email",mail)<=0){
				if(noteUserService.exist("user_name",userName)<=0){
					String code=String.valueOf((int)Math.random()*10000);
					//发送验证码
					code="1234";
					//写入到session
					session.setAttribute("dentifycode",code);
					map.put("userid",String.valueOf(noteUserService.exist("user_name",userName)));
					map.put("mail",String.valueOf(noteUserService.exist("user_email",mail)));
					map.put("result","1");
					map.put("message","验证码发送成功");
				}
				else{
					map.put("userid","");
					map.put("result","2");
					map.put("message","用户名已存在");
				}
			}
			else{
				map.put("userid","");
				map.put("result","3");
				map.put("message","邮箱已被注册");
			}
		}else{
			map.put("userid","");
			map.put("result","4");
			map.put("message","参数错误");
		}
		return map;
	}

	//注册用户
	//@RequiresPermissions("noteuser:user:edit")
	@RequestMapping(value = "saveUser")
	@ResponseBody
	public Map<String,String> saveUser(String userName, String password,String mail,String code,HttpServletRequest request, HttpServletResponse response) {
		//获取当前会话
		HttpSession session=request.getSession();
		String identifycode=(String)session.getAttribute("dentifycode");
		Map<String, String> map = new HashMap<String, String>();
		if (userName != null && password != null && mail != null &&code!=null) {
			if(identifycode!=null&&identifycode.equals(code)){
				noteUserService.create(userName,password,mail);
				NoteUser newUser=new NoteUser();
				map.put("userid","");
				map.put("result","1");
				map.put("message","注册成功");
			}else{
				map.put("userid", "");
				map.put("result", "3");
				map.put("message", "验证码错误");
			}
		}else{
				map.put("userid", "");
				map.put("result", "4");
				map.put("message", "参数错误");
			}
			return map;
	}

	//修改密码_发送验证码identifycode
	//@RequiresPermissions("noteuser:user:edit")
	@RequestMapping(value = "sendCodeUpdate")
	@ResponseBody
	public Map<String,String> sendCodeUpdate(String userName,HttpServletRequest request, HttpServletResponse response) {
		//获取当前会话
		HttpSession session=request.getSession();
		Map<String, String> map = new HashMap<String, String>();
		//String identifycode=(String)session.getAttribute("modify_password_dentifycode");
		if (userName != null  ) {
			String userMail=noteUserService.getMail(userName);
			if(userMail!=null){
				//生成随机数
				String code=String.valueOf((int)Math.random()*10000);
				//发送email
				code="1243";
				//写入会话
				session.setAttribute("modify_password_dentifycode",code);

				map.put("userMail", userMail);
				map.put("result", "1");
				map.put("message", "发送到用户邮箱");
			}
			map.put("result", "1");
			map.put("message", "发送到用户邮箱");
		}else {
			map.put("userid", "");
			map.put("result", "4");
			map.put("message", "参数错误");
		}
		return map;
	}



	//修改密码
	//@RequiresPermissions("noteuser:noteUser:edit")
	@RequestMapping(value = "modify_password")
	@ResponseBody
	public Map<String,String> modify_password(String userName, String password,String identifycode,HttpServletRequest request) {
		logger.debug("come to modify_password");
		HttpSession session=request.getSession();
		Map<String,String> map=new HashMap<String, String>();
		String modify_password_dentifycode=(String)session.getAttribute("modify_password_dentifycode");
		if(userName!=null&&password!=null&&identifycode!=null){
			if(identifycode.equals(modify_password_dentifycode)){//
				//通过 修改password的值
				noteUserService.update_password(userName,password);
				map.put("result","1");
				map.put("message","modify success");
			}else{
				map.put("result","0");
				map.put("message","验证码有误");
			}
		}else{
			map.put("result","2");
			map.put("message","参数错误");
		}
		return map;
	}


}